import csv
import os
import time
import requests
from bs4 import BeautifulSoup
from io import BytesIO
from threading import Thread
from PIL import Image
from queue import Queue


def produce(q: Queue, url):
	headers = {
		'Referer': 'https://music.163.com',
		'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36 Edg/107.0.1418.52'
	}
	req = requests.get(url, headers)
	page = BeautifulSoup(req.text, 'html.parser')
	info = page.select(".dec a")
	q.put(info)


def consume(q: Queue):
	row_title = [
		'id', 'title', 'maker_name', 'desc', 'music_number', 'play_count',
		'add_list_count', 'share_count', 'comments_count'
	]
	file = open(
		r'D:\buaa_czw\2022 Autumn\General Programming 2022\Week 12\Results\res.csv',
		'w',
		encoding='utf-8'
		)
	csv_file = csv.writer(file)
	csv_file.writerow(row_title)
	headers = {
		'Referer': 'https://music.163.com',
		'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36 Edg/107.0.1418.52'
	}
	playlists = q.get()
	if playlists is None:
		file.close()
		return
	for plist in playlists:
		url = r'https://music.163.com' + plist['href']
		req = requests.get(url, headers)
		page = BeautifulSoup(req.text, 'html.parser')
		pid = page.select('.s-fc7')[0]['href'].split('=')[-1]
		# 歌单id
		img_url = page.select('img')[0]['data-src']
		pimg = Image.open(BytesIO(requests.get(img_url).content))
		try:
			pimg.save(str(time.time()) + '.jpg')
		except OSError:
			pimg.save(str(time.time()) + '.png')
		# 歌单封面图及保存
		ptitle = page.select('title')[0].get_text().split(' - ')[0]
		# 歌单题目
		makername = page.select('.s-fc7')[0].get_text()
		# 创建者名称
		pdes = page.select('p')[0].get_text()
		# 歌单介绍
		pcount = page.select('strong')[0].get_text()
		# 歌单播放数
		pmusicnum = page.select('span span')[0].get_text()
		# 歌单歌曲数
		paddcount = page.select('a i')[1].get_text()
		# 歌单被收藏数
		psharecount = page.select('a i')[2].get_text()
		# 歌单被分享次数
		pcommentcount = page.select('a i')[4].get_text()
		# 歌单被评论次数
		csv_file.writerow(
			[pid, ptitle, makername, pdes, pmusicnum, pcount, paddcount, psharecount, pcommentcount]
		)
		time.sleep(20)
	file.close()


def main():
	url_list = []
	plist = []
	clist = []

	q = Queue()
	for i in range(0, 70, 35):
		url = f'https://music.163.com/discover/playlist/?order=hot&cat=%E6%97%A5%E8%AF%AD&limit=35&offset={i}'
		url_list.append(url)

	for url in url_list:
		producer = Thread(target=produce, args=(q, url, ))
		plist.append(producer)
	for prod in plist:
		prod.start()
		time.sleep(10)
	for thread in plist:
		thread.join()

	for i in range(os.cpu_count()):
		consumer = Thread(target=consume, args=(q, ))
		clist.append(consumer)
	for cons in clist:
		cons.start()
	for c in clist:
		q.put(None)


if __name__ == '__main__':
	start_time = time.time()
	main()
	print(f"运行时间：{time.time() - start_time}")
